/*     */ package com.cr.web.controller.system;
/*     */ 
/*     */ import com.cr.common.annotation.Log;
/*     */ import com.cr.common.core.controller.BaseController;
/*     */ import com.cr.common.core.domain.AjaxResult;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.core.page.TableDataInfo;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.enums.BusinessType;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import com.cr.common.utils.poi.ExcelUtil;
/*     */ import com.cr.framework.shiro.service.SysPasswordService;
/*     */ import com.cr.system.service.ISysPostService;
/*     */ import com.cr.system.service.ISysRoleService;
/*     */ import com.cr.system.service.ISysUserService;
/*     */ import java.util.List;
/*     */ import java.util.stream.Collectors;
/*     */ import org.apache.commons.lang3.ArrayUtils;
/*     */ import org.apache.shiro.authz.annotation.RequiresPermissions;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.ui.ModelMap;
/*     */ import org.springframework.validation.annotation.Validated;
/*     */ import org.springframework.web.bind.annotation.GetMapping;
/*     */ import org.springframework.web.bind.annotation.PathVariable;
/*     */ import org.springframework.web.bind.annotation.PostMapping;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/system/user"})
/*     */ public class SysUserController extends BaseController {
/*  43 */   private String prefix = "system/user";
/*     */   
/*     */   @Autowired
/*     */   private ISysUserService userService;
/*     */   
/*     */   @Autowired
/*     */   private ISysRoleService roleService;
/*     */   
/*     */   @Autowired
/*     */   private ISysPostService postService;
/*     */   
/*     */   @Autowired
/*     */   private SysPasswordService passwordService;
/*     */   
/*     */   @RequiresPermissions({"system:user:view"})
/*     */   @GetMapping
/*     */   public String user() {
/*  61 */     return this.prefix + "/user";
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:list"})
/*     */   @PostMapping({"/list"})
/*     */   @ResponseBody
/*     */   public TableDataInfo list(SysUser user) {
/*  69 */     startPage();
/*  70 */     List<SysUser> list = this.userService.selectUserList(user);
/*  71 */     return getDataTable(list);
/*     */   }
/*     */   
/*     */   @Log(title = "用户管理", businessType = BusinessType.EXPORT)
/*     */   @RequiresPermissions({"system:user:export"})
/*     */   @PostMapping({"/export"})
/*     */   @ResponseBody
/*     */   public AjaxResult export(SysUser user) {
/*  80 */     List<SysUser> list = this.userService.selectUserList(user);
/*  81 */     ExcelUtil<SysUser> util = new ExcelUtil(SysUser.class);
/*  82 */     return util.exportExcel(list, "用户数据");
/*     */   }
/*     */   
/*     */   @Log(title = "用户管理", businessType = BusinessType.IMPORT)
/*     */   @RequiresPermissions({"system:user:import"})
/*     */   @PostMapping({"/importData"})
/*     */   @ResponseBody
/*     */   public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
/*  91 */     ExcelUtil<SysUser> util = new ExcelUtil(SysUser.class);
/*  92 */     List<SysUser> userList = util.importExcel(file.getInputStream());
/*  93 */     String message = this.userService.importUser(userList, Boolean.valueOf(updateSupport), getLoginName());
/*  94 */     return AjaxResult.success(message);
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:view"})
/*     */   @GetMapping({"/importTemplate"})
/*     */   @ResponseBody
/*     */   public AjaxResult importTemplate() {
/* 102 */     ExcelUtil<SysUser> util = new ExcelUtil(SysUser.class);
/* 103 */     return util.importTemplateExcel("用户数据");
/*     */   }
/*     */   
/*     */   @GetMapping({"/add"})
/*     */   public String add(ModelMap mmap) {
/* 112 */     mmap.put("roles", this.roleService.selectRoleAll().stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
/* 113 */     mmap.put("posts", this.postService.selectPostAll());
/* 114 */     return this.prefix + "/add";
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:add"})
/*     */   @Log(title = "用户管理", businessType = BusinessType.INSERT)
/*     */   @PostMapping({"/add"})
/*     */   @ResponseBody
/*     */   public AjaxResult addSave(@Validated SysUser user) {
/* 126 */     if ("1".equals(this.userService.checkLoginNameUnique(user.getLoginName())))
/* 128 */       return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在"); 
/* 130 */     if (StringUtils.isNotEmpty(user.getPhonenumber()) && "1"
/* 131 */       .equals(this.userService.checkPhoneUnique(user)))
/* 133 */       return error("新增用户'" + user.getLoginName() + "'失败，手机号码已存在"); 
/* 135 */     if (StringUtils.isNotEmpty(user.getEmail()) && "1"
/* 136 */       .equals(this.userService.checkEmailUnique(user)))
/* 138 */       return error("新增用户'" + user.getLoginName() + "'失败，邮箱账号已存在"); 
/* 140 */     user.setSalt(ShiroUtils.randomSalt());
/* 141 */     user.setPassword(this.passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
/* 142 */     user.setCreateBy(getLoginName());
/* 143 */     return toAjax(this.userService.insertUser(user));
/*     */   }
/*     */   
/*     */   @GetMapping({"/edit/{userId}"})
/*     */   public String edit(@PathVariable("userId") Long userId, ModelMap mmap) {
/* 152 */     this.userService.checkUserDataScope(userId);
/* 153 */     List<SysRole> roles = this.roleService.selectRolesByUserId(userId);
/* 154 */     mmap.put("user", this.userService.selectUserById(userId));
/* 155 */     mmap.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
/* 156 */     mmap.put("posts", this.postService.selectPostsByUserId(userId));
/* 157 */     return this.prefix + "/edit";
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:edit"})
/*     */   @Log(title = "用户管理", businessType = BusinessType.UPDATE)
/*     */   @PostMapping({"/edit"})
/*     */   @ResponseBody
/*     */   public AjaxResult editSave(@Validated SysUser user) {
/* 169 */     this.userService.checkUserAllowed(user);
/* 170 */     if (StringUtils.isNotEmpty(user.getPhonenumber()) && "1"
/* 171 */       .equals(this.userService.checkPhoneUnique(user)))
/* 173 */       return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在"); 
/* 175 */     if (StringUtils.isNotEmpty(user.getEmail()) && "1"
/* 176 */       .equals(this.userService.checkEmailUnique(user)))
/* 178 */       return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在"); 
/* 180 */     user.setUpdateBy(getLoginName());
/* 181 */     return toAjax(this.userService.updateUser(user));
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:resetPwd"})
/*     */   @GetMapping({"/resetPwd/{userId}"})
/*     */   public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap) {
/* 188 */     mmap.put("user", this.userService.selectUserById(userId));
/* 189 */     return this.prefix + "/resetPwd";
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:resetPwd"})
/*     */   @Log(title = "重置密码", businessType = BusinessType.UPDATE)
/*     */   @PostMapping({"/resetPwd"})
/*     */   @ResponseBody
/*     */   public AjaxResult resetPwdSave(SysUser user) {
/* 198 */     this.userService.checkUserAllowed(user);
/* 199 */     user.setSalt(ShiroUtils.randomSalt());
/* 200 */     user.setPassword(this.passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
/* 201 */     if (this.userService.resetUserPwd(user) > 0) {
/* 203 */       if (ShiroUtils.getUserId().longValue() == user.getUserId().longValue())
/* 205 */         setSysUser(this.userService.selectUserById(user.getUserId())); 
/* 207 */       return success();
/*     */     } 
/* 209 */     return error();
/*     */   }
/*     */   
/*     */   @GetMapping({"/authRole/{userId}"})
/*     */   public String authRole(@PathVariable("userId") Long userId, ModelMap mmap) {
/* 218 */     SysUser user = this.userService.selectUserById(userId);
/* 220 */     List<SysRole> roles = this.roleService.selectRolesByUserId(userId);
/* 221 */     mmap.put("user", user);
/* 222 */     mmap.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
/* 223 */     return this.prefix + "/authRole";
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:edit"})
/*     */   @Log(title = "用户管理", businessType = BusinessType.GRANT)
/*     */   @PostMapping({"/authRole/insertAuthRole"})
/*     */   @ResponseBody
/*     */   public AjaxResult insertAuthRole(Long userId, Long[] roleIds) {
/* 235 */     this.userService.insertUserAuth(userId, roleIds);
/* 236 */     return success();
/*     */   }
/*     */   
/*     */   @RequiresPermissions({"system:user:remove"})
/*     */   @Log(title = "用户管理", businessType = BusinessType.DELETE)
/*     */   @PostMapping({"/remove"})
/*     */   @ResponseBody
/*     */   public AjaxResult remove(String ids) {
/* 245 */     if (ArrayUtils.contains((Object[])Convert.toLongArray(ids), getUserId()))
/* 247 */       return error("当前用户不能删除"); 
/* 249 */     return toAjax(this.userService.deleteUserByIds(ids));
/*     */   }
/*     */   
/*     */   @PostMapping({"/checkLoginNameUnique"})
/*     */   @ResponseBody
/*     */   public String checkLoginNameUnique(SysUser user) {
/* 259 */     return this.userService.checkLoginNameUnique(user.getLoginName());
/*     */   }
/*     */   
/*     */   @PostMapping({"/checkPhoneUnique"})
/*     */   @ResponseBody
/*     */   public String checkPhoneUnique(SysUser user) {
/* 269 */     return this.userService.checkPhoneUnique(user);
/*     */   }
/*     */   
/*     */   @PostMapping({"/checkEmailUnique"})
/*     */   @ResponseBody
/*     */   public String checkEmailUnique(SysUser user) {
/* 279 */     return this.userService.checkEmailUnique(user);
/*     */   }
/*     */   
/*     */   @Log(title = "用户管理", businessType = BusinessType.UPDATE)
/*     */   @RequiresPermissions({"system:user:edit"})
/*     */   @PostMapping({"/changeStatus"})
/*     */   @ResponseBody
/*     */   public AjaxResult changeStatus(SysUser user) {
/* 291 */     this.userService.checkUserAllowed(user);
/* 292 */     return toAjax(this.userService.changeStatus(user));
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\classes\com.zip!\com\cr\web\controller\system\SysUserController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */